package main.java.tmi.product.web;

import java.sql.SQLException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import main.java.tmi.common.util.AjaxPageSplit;
import main.java.tmi.product.service.ProductService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;


/**
 *  Controller
 *
 * @author min
 * @since 2010. 09. 08.
 */
@Controller
public class ProductController {
	
    @Resource(name = "productServiceImpl")
    private ProductService productService;
    
    /**
     * The Constructor
     */
    public ProductController() {
        //logger = LogFactory.getLog(this.getClass());
    }

    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/product")
    public String viewList(HttpServletRequest request, HttpServletResponse response,
            ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============product start=========================");
        System.out.println("====================================================");

        return "/tmi/product/productList";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/productDetail")
    public String viewDetail(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam Map param, ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============productDetail start=========================");
        System.out.println("====================================================");
        model.addAttribute("goodsCode",param.get("goodsCode"));
        return "/tmi/product/productDetail";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/productWrite")
    public String viewWrite(HttpServletRequest request, HttpServletResponse response,
            ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============productWrite start=========================");
        System.out.println("====================================================");

        return "/tmi/product/productWrite";
    }
    
    /**
     * 
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    @RequestMapping("/productUpdate")
    public String viewUpdate(HttpServletRequest request, HttpServletResponse response,
    		@RequestParam Map param, ModelMap model) throws SQLException {
        System.out.println("====================================================");
        System.out.println("===============productUpdate start=========================");
        System.out.println("====================================================");
        System.out.println("==========================param=========================="+param);
        model.addAttribute("goodsCode",param.get("goodsCode"));
        return "/tmi/product/productUpdate";
    }
    
    /**
     * search
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    
    @RequestMapping("/getProductView")
    public void getProductView(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> getProductView start");
        AjaxPageSplit pageSplitMgr = new AjaxPageSplit();
        int pagingTmp = Integer.parseInt(param.get("paging").toString());
		int listScale = Integer.parseInt(param.get("listScale").toString());
		int paging = (pagingTmp-1)*listScale;
		param.put("paging", paging);
        int productCount = productService.getProductViewCount(param);
		String pageSplit = pageSplitMgr.getSplitPageLink(productCount, pagingTmp, listScale, 5, "");
		System.out.println("===========pageSplit============> "+pageSplit);
		
		model.addAttribute("pageSplit",pageSplit);
		model.addAttribute("productCount",productCount);
		model.addAttribute("resultList",productService.getProductView(param));
    }
    
    /**
     * Detail search
     * 
     * @param HttpServletResponse response
     * @param ModelMap model
     * @return
     * @throws SQLException
     */
    
    @RequestMapping("/getProductDetailView")
    public void getProductDetailView(@RequestParam Map param, Model model) throws Exception {
        System.out.println("=======================> getProductDetailView start");
        System.out.println("getProductDetailView start=======================>"+param);
        
        //조회
		model.addAttribute("resultDetailMap",productService.getProductDetailView(param));
    }
    
    /**
     * order SAVE
     */
    @RequestMapping("/saveProduct")
    public void saveProduct(@RequestParam Map param, Model model) throws Exception {
    	System.out.println("=======================> saveProduct Start");
    	
    	boolean resultData = productService.addProduct(param);

        //저장 완료 후 조회
        model.addAttribute("result",resultData);
        
    }
    
    /**
     * order de
     */
    @RequestMapping("/delProduct")
    public void delProduct(@RequestParam Map param, Model model) throws Exception {
    	System.out.println("=======================> delProduct Start");
    	
    	boolean resultData = productService.delProduct(param);

        //저장 완료 후 조회
        model.addAttribute("result",resultData);
        
    }
}
